package org.jeecg.modules.inOrderTaskMain.service.impl;

import org.jeecg.modules.inOrderTaskMain.entity.InOrderTaskMain;
import org.jeecg.modules.inOrderTaskMain.entity.InOrderTaskDetail;
import org.jeecg.modules.inOrderTaskMain.mapper.InOrderTaskDetailMapper;
import org.jeecg.modules.inOrderTaskMain.mapper.InOrderTaskMainMapper;
import org.jeecg.modules.inOrderTaskMain.service.IInOrderTaskMainService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 入库任务
 * @Author: jeecg-boot
 * @Date:   2024-10-08
 * @Version: V1.0
 */
@Service
public class InOrderTaskMainServiceImpl extends ServiceImpl<InOrderTaskMainMapper, InOrderTaskMain> implements IInOrderTaskMainService {

	@Autowired
	private InOrderTaskMainMapper inOrderTaskMainMapper;
	@Autowired
	private InOrderTaskDetailMapper inOrderTaskDetailMapper;
	
	@Override
	@Transactional
	public void saveMain(InOrderTaskMain inOrderTaskMain, List<InOrderTaskDetail> inOrderTaskDetailList) {
		inOrderTaskMainMapper.insert(inOrderTaskMain);
		if(inOrderTaskDetailList!=null && inOrderTaskDetailList.size()>0) {
			for(InOrderTaskDetail entity:inOrderTaskDetailList) {
				//外键设置
				entity.setInOrderTaskId(inOrderTaskMain.getId());
				inOrderTaskDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(InOrderTaskMain inOrderTaskMain,List<InOrderTaskDetail> inOrderTaskDetailList) {
		inOrderTaskMainMapper.updateById(inOrderTaskMain);
		
		//1.先删除子表数据
		inOrderTaskDetailMapper.deleteByMainId(inOrderTaskMain.getId());
		
		//2.子表数据重新插入
		if(inOrderTaskDetailList!=null && inOrderTaskDetailList.size()>0) {
			for(InOrderTaskDetail entity:inOrderTaskDetailList) {
				//外键设置
				entity.setInOrderTaskId(inOrderTaskMain.getId());
				inOrderTaskDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		inOrderTaskDetailMapper.deleteByMainId(id);
		inOrderTaskMainMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			inOrderTaskDetailMapper.deleteByMainId(id.toString());
			inOrderTaskMainMapper.deleteById(id);
		}
	}
	
}
